/*
 * YuebaoMagazineItem.cpp
 *
 *  Created on: 2011-3-11
 *      Author: 
 */

#include "YuebaoMagazineItem.h"
#include "fWml.h"
#include "ProjectsDefines.h"
#include "XmlNodeCreater.h"
#include "BetterUserHBufC.h"

YuebaoMagazineItem* YuebaoMagazineItem::NewLC()
{
    YuebaoMagazineItem* self = new ( ELeave ) YuebaoMagazineItem( );
    CleanupStack::PushL( self );
    self->ConstructL( );
    return self;
}

YuebaoMagazineItem* YuebaoMagazineItem::NewL()
{
    YuebaoMagazineItem* self = YuebaoMagazineItem::NewLC( );
    CleanupStack::Pop( ); // self;
    return self;
}

YuebaoMagazineItem::YuebaoMagazineItem()
{

}

void YuebaoMagazineItem::ConstructL()
{
    _last_read = kNewMagazine;
}

YuebaoMagazineItem::~YuebaoMagazineItem()
{
    SAFE_DELETE( _name );
    SAFE_DELETE( _data_file );
    SAFE_DELETE(_add_time);
}
_LIT( kGoodsIDNode, "id");
_LIT( kGoodsNameNode, "name");
_LIT( kGoodsDataNode, "data_file");
_LIT( kGoodsAddTimeNode, "add_time");
_LIT( kGoodsLastReadNode, "last_read");

YuebaoMagazineItem* YuebaoMagazineItemParser::Parse(CElement* elementGoods)
{
    if( elementGoods == 0 || elementGoods->ElementCount( ) == 0 )
    {
        return NULL;
    }
    YuebaoMagazineItem* goods = YuebaoMagazineItem::NewLC( );

    goods->_id = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kGoodsIDNode ) );
    goods->_name = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsNameNode ) );
    goods->_data_file = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsDataNode ) );
    goods->_add_time = BetterUtility::GetElementText( elementGoods->GetElement( kGoodsAddTimeNode ) );
    goods->_last_read = BetterUtility::GetElementTextAsInt( elementGoods->GetElement( kGoodsLastReadNode ) );

    CleanupStack::Pop( ); // goods;
    return goods;
}
_LIT( kGoodsNodeStart,"<magazine>" );
_LIT( kGoodsNodeEnd ,"</magazine>");

void YuebaoMagazineItemParser::BuildXmlGoodsNode(YuebaoMagazineItem* goods, TDes& buffer)
{
    //buffer.SetLength( 0 );
    if( !goods )
    {
        return;
    }

    buffer.Append( kGoodsNodeStart );
    // <node>value</node>
    // <node/>

    // id
    XmlNodeCreater::BuildIntNode( kGoodsIDNode, goods->_id, buffer );
    // name
    XmlNodeCreater::BuildTextNode( kGoodsNameNode, goods->_name, buffer );
    // data_file
    XmlNodeCreater::BuildTextNode( kGoodsDataNode, goods->_data_file, buffer );
    // add_time
    XmlNodeCreater::BuildTextNode( kGoodsAddTimeNode, goods->_add_time, buffer );
    // last read
    XmlNodeCreater::BuildIntNode( kGoodsLastReadNode, goods->_last_read, buffer );
    buffer.Append( kGoodsNodeEnd );
}
